<template>
  <div class="app-container bg-gray">
    <el-card class="box-1">
      <div class="header">
        <div/>
        <div>
          <el-button type="primary" icon="el-icon-plus" @click="toAdd">添加类型</el-button>
        </div>
      </div>
      <el-table v-loading="tableLoading" :data="tableData" :max-height="tableMaxHeight" border highlight-current-row @sort-change="handleSortChange">
        <el-table-column prop="id" label="ID" width="120" header-align="center" align="center" sortable />
        <el-table-column prop="name" maxlength="20" label="类型名称" min-width="160"/>
        <el-table-column label="操作" width="200" header-align="center" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button type="success" size="small" icon="el-icon-edit" circle class="bg-green" @click="toEdit(scope.row)"/>
            <el-button type="danger" size="small" icon="el-icon-delete" circle @click="toDelete(scope.row)"/>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog :visible.sync="dialogVisible" :title="form.title" width="500px" class="dialog" center @close="resetDialog('form')">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" autofocus/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitDialog('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { handleSortChange, handleSizeChange, handleCurrentChange, parseTime } from '@/utils/table'

export default {
  data() {
    return {
      downloadLoading: false,

      // 表格相关数据
      tableMaxHeight: document.documentElement.clientHeight - 380,
      tableLoading: false,
      tableData: [],

      // 弹出框相关数据
      dialogVisible: false,
      form: {
        title: '',
        name: ''
      },
      rules: {
        name: [{ required: true, message: '请输入产品类型名称', trigger: 'change' }]
      }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    handleCurrentChange, handleSizeChange, handleSortChange, parseTime,
    getData(order) {
      this.tableLoading = true
      order || (order = {})
      const url = 'vip/typeList'
      this.$store.dispatch('GetConnect', { url }).then(res => {
        this.tableData = res.data
        this.tableLoading = false
      }).catch(e => {
        this.tableLoading = false
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    toAdd() {
      this.form = {}
      this.dialogVisible = true
      this.form.title = '添加类型'
    },
    toEdit(row) {
      this.form = {}
      this.dialogVisible = true
      this.form = {
        id: row.id,
        name: row.name
      }
      this.form.title = '编辑类型'
    },
    /**
     * 弹窗表单提交方法,通过判断有没有form_id执行添加或编辑操作
     * formName: form表单中ref的值,即表单名
     */
    submitDialog(formName) {
      this.$refs[formName].validate((valid) => { // 表单验证
        if (valid) {
          const url = this.form['id'] ? 'vip/updateType' : 'vip/addType'
          const data = this.form
          this.$store.dispatch('GetConnect', { url, data }).then(res => {
            this.dialogVisible = false
            this.$message.success(res.msg)
            this.getData()
          }).catch(e => {
            this.$message.error(e.msg + ',请刷新或联系管理员!')
          })
        } else {
          this.$message.error('提交失败,请检查必填项')
        }
      })
    },
    resetDialog(formName) {
      this.$refs[formName].resetFields()
    },

    toDelete(row) {
      this.$confirm('此操作将删除该类型, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const url = 'vip/delType/' + row.id
        const data = {
          id: row.id
        }
        this.$store.dispatch('GetConnect', { url, data }).then(res => {
          this.$message.success(res.msg)
          this.getData()
        }).catch(e => {
          this.$message.error(e.msg + ',请刷新或联系管理员!')
        })
      })
    }
  }
}
</script>

<style lang="scss" type="text/scss" scoped>
@import '../../../styles/index.scss';
.box-1 {
  .model {
    .el-card {
      color: #fff;
      background-color: $panGreen;
      span {
        font-size: 14px;
      }
      div {
        font-size: 24px;
        margin-top: 10px;
      }
    }
  }
  .header {
    @include flex-row(center, space-between);
    flex-wrap: wrap;
    padding-bottom: 15px;
    div {
      @include flex-row(center, flex-start);
      flex-wrap: wrap;
      .select {
        padding-right: 15px;
        width: 150px;
      }
      .picker {
        margin-right: 15px;
        width: 300px;
      }
      .do-btn {
        @extend .bg-green;
        margin-left: 6px;
      }
      .search-input {
        width: 180px;
      }
    }
  }
}
</style>

